<template>
  <div class="page-login">
    <el-row type="flex" justify="center">
      <el-col style="width: 250px">
        <el-row type="flex" align="middle" class="row">
          <label class="label">用户</label>
          <el-input v-model="name" style="width: 200px"></el-input>
        </el-row>
        <el-row type="flex" align="middle" class="row">
          <label class="label">密码</label>
          <el-input v-model="password" type="password" style="width: 200px"></el-input>
        </el-row>
        <el-row type="flex" align="middle" class="row">
          <el-button type="primary" @click="handleClick" style="width: 250px">登录</el-button>
        </el-row>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      name: "", // 绑定用户名变量
      password: "" // 绑定密码变量
    };
  },
  methods: {
    handleClick() {
      // 发送登录请求
      this.axios
        .post("/v1/login", {
          name: this.name,
          password: this.password
        })
        .then(res => {
          // 如果登录成功
          if (res.data.code === 1) {
            this.$router.push({ path: "/" }); // 跳转到首页
          } else {
            alert(res.data.msg); // 如果失败，就将服务端返回的提示信息展示出来
          }
        });
    }
  }
};
</script>
<style scoped>
.page-login {
  padding-top: 200px;
}
.row {
  width: 250px;
  margin-bottom: 20px;
}
.label {
  width: 50px;
  text-align: left;
}
</style>
